
<template>
  <div class="register_container" :style="backImgStyle">
    <div class="register_x">
      <div class="inner_box">
        <el-form ref="registerForm" :rules="registerRules" :model="registerForm" class="register-form" auto-complete="on"
                 label-position="left">
          <div class="title-container">
            <div class="register_header">
              <h3 >注册</h3>
            </div>
            <div style="margin-left: 50%">已有账号，
              <a href=""@click.prevent="toLogin"style="color: #22a2c3">前往登录</a>
            </div>
          </div>
          <div class="register_content">
            <div  class="body_item">
              <el-form-item prop="phoneNumber">

                <el-input ref="mobile" v-model="registerForm.phoneNumber" placeholder="请输入手机号" name="phoneNumber"
                          type="text"
                          tabindex="1" auto-complete="on"/>
              </el-form-item>
              <el-form-item prop="code">

                <el-input ref="registerCode" v-model="registerForm.registerCode" type="text" placeholder="请输入验证码" name="registerCode"
                          abindex="2"
                          maxlength="6" auto-complete="on" @keyup.enter.native="register"/>
                <span class="show-pwd">
                  <el-button :loading="sending" :disabled="sendDisabled" size="small"
                             @click="onSendSms">{{ sendButtonText }}</el-button>
                </span>
              </el-form-item>
            </div>
          </div>
          <el-button :loading="loading"  style="width:100%;margin-bottom:20px;color:#ffffff;background-color: #4e7ca1"
                     @click.native.prevent="register">注册
          </el-button>

        </el-form>
      </div>
    </div>
  </div>

</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      backImgStyle: {
        backgroundImage: "url(https://eladmin.vip/demo/static/img/background.df9c4cdb.webp)"
      },
      registerForm: {
        phoneNumber: "",// 手机号
        registerCode: "",// 验证码

      },
      registerRules: {
        phoneNumber: [{required: true, message: "手机号不能为空！", trigger: "blur"},
          {pattern: /^1[3456789]\d{9}$/, message: '手机号码格式不正确', trigger: 'blur'},
          {min: 11, max: 11, message: "请输入11位的手机号！", trigger: 'blur'}],

        registerCode: [{required: true, message: "验证码不能为空！", trigger: "blur"}]
      },
      cur: true,//true代表密码, false代表短信
      sending: false,
      sendDisabled: false,
      loading: false,
      timer: 0,
      passwordType: 'password',

    };
  },
  computed: {
    sendButtonText() {
      if (this.timer === 0) {
        this.sending = false;
        return "发送验证码";
      } else {
        return `${this.timer}秒后重发`;
      }
    }
  },
  watch: {

    timer(num) {
      if (num > 0) {
        setTimeout(() => {
          this.timer = --num;
        }, 1000);
      }
    },

  },
  created() {

  },

  methods: {
    onSendSms() {
      this.registerWay = false;
      this.$refs.registerForm.validateField("phoneNumber", err => {
        if (!err) {
          this.$axios.postForm("/user/sendRegisterCode", this.registerForm).then(respones => {
            if (respones.data.code == 200) {
              this.sending = true;
              const {phoneNumber} = this.registerForm;
              this.$message({
                message: "短信已发送成功，请注意查收",
                type: "success",
                duration: 2000,
              })
              this.timer = 60;
              this.sending=true;
            } else {
              this.$message({
                message: "网络异常！请稍后再试",
                type: "error",
                duration: 2000,
              })
            }

          })
        }
      });
    },
    register() {
      this.$refs.registerForm.validate(isValid => {
        if (!isValid) {
          this.$message({
            message: "请正确填写",
            type: "error",
            duration: 2000,
          })
          return;
        } else {
          this.$axios.postForm("/user/register", this.registerForm).then(respones => {
            if (respones.data.code == 200) {
              this.$alert("注册成功，您的密码为"+respones.data.data+"！请牢记！")
              this.$router.push("/Login")
            } else {
              this.$message({
                message: respones.data.msg,
                type: "error",
                duration: 2000,
              })
            }
          })
        }
      })
    },
    toLogin() {
      this.$router.push("/Login")
    },
  }
}
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}

.register_container {
  background-color: #2b4b6b;
  height: 100%;
}

.register_x {
  background-color: #b2bbbe;
  width: 27%;
  height: 340px;
  border-radius: 9px;
  position: absolute;
  left: 50.4%;
  top: 46%;
  transform: translate(-50%, -50%);
  filter:opacity(80%)
}

.inner_box {
  margin-top: 30px;
  margin-left: 11%;
  width: 80%;
  height: 450px;
}

.title {
  color: #000000;
  text-align: center;
}

.login_header {
  //padding-bottom: 10px;

  text-align: center;
  line-height: 50px;
}

.login_header span {
  margin-right: 20px;
  cursor: pointer;
}

.body_item {
  margin-top: 20px;
  border: 0px solid #999;
  overflow: hidden;
}

.active {
  color: #4e7ca1;
  padding-bottom: 10px;
  border-bottom: 3px solid #4e7ca1;
}

</style>